<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 管理员日志
 * +----------------------------------------------------------------------
 */
namespace app\admin\controller;

use app\admin\model\AdminLog as AdminLogModel;

use think\facade\Config;
use think\facade\View;
use think\facade\Db;

class AdminLog extends Base
{
  // 管理员日志列表
  public function index() 
  {
    $this->delete();
    $model = new AdminLogModel();
    $post = $this->request->param();
    if (isset($post['admin_id']) and $post['admin_id'] > 0) {
      $where[] = ['admin_id','=',$post['admin_id']];
    }

    if(isset($post['create_time']) and !empty($post['create_time'])) {
      $min_time = strtotime($post['create_time']);
      $max_time = $min_time + 24 * 60 * 60;
      $where[] = ['create_time','between',[$min_time,$max_time]];
    }
     
    $log = empty($where) ? $model->order('create_time desc')->paginate(20) : $model->where($where)->order('create_time desc')->paginate(20,false,['query'=>request()->get()]);
    $info['admin'] = Db::name('admin')->select();
    View::assign([
      'log' => $log,
      'info' => $info
    ]);
    return View::fetch();
  }

  // 管理员日志删除
  public function delete() {
    $num = Config::get('admin.admin_log_num'); // 日志最大保存上限
    $count = AdminLogModel::count();
    if ($count > $num) {
      $max = AdminLogModel::max('id');
      AdminLogModel::where('id', '<=', $max-$num)->delete();
    }
  }
}

?>